package org.fanrui.hrm.service.impl;

import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.fanrui.hrm.constants.ErrorCode.CustomizeErrorCode;
import org.fanrui.hrm.constants.employee.EmployeeType;
import org.fanrui.hrm.constants.tenant.TenantStateConstants;
import org.fanrui.hrm.domain.Employee;
import org.fanrui.hrm.domain.Login;
import org.fanrui.hrm.domain.Tenant;
import org.fanrui.hrm.dto.AuditDto;
import org.fanrui.hrm.dto.LoginMealDto;
import org.fanrui.hrm.dto.TenantRegisterDto;
import org.fanrui.hrm.feign.clients.LoginFeignClients;
import org.fanrui.hrm.mapper.TenantMapper;
import org.fanrui.hrm.service.IEmployeeService;
import org.fanrui.hrm.service.ITenantService;
import org.fanrui.hrm.util.AjaxResult;
import org.fanrui.hrm.util.AssertUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author fanrui
 * @since 2024-09-10
 */
@Service
@Slf4j
public class TenantServiceImpl extends ServiceImpl<TenantMapper, Tenant> implements ITenantService {

    @Resource
    private LoginFeignClients loginFeignClients;
    @Resource
    private ITenantService tenantService;
    @Resource
    private IEmployeeService employeeService;

    /**
     * 机构入驻
     * <p>
     * 参数校验 （基本校验，重复校验）
     * 保存 login 密码加密加盐
     * 保存机构和套餐之间的关系表
     * 保存 employee
     * 保存 tenant
     *
     * @param tenantRegisterDto 入驻的信息
     */
    @Override
    @Transactional(rollbackFor = {Exception.class}) //需要加分布式事务
    public void entering(TenantRegisterDto tenantRegisterDto) {
        // 参数校验
        Login login = tenantRegisterDto.getLogin();
        Tenant tenant = tenantRegisterDto.getTenant();
        Long mealId = tenantRegisterDto.getMealId();
        Employee employee = tenantRegisterDto.getEmployee();

        // 保存 login 密码加密加盐,保存机构和套餐之间的关系表
        LoginMealDto loginMealDto = new LoginMealDto();
        loginMealDto.setLogin(login);
        loginMealDto.setMealId(mealId);

        AjaxResult result = loginFeignClients.saveLoginAndMealId(loginMealDto);
        AssertUtil.notObjectNull(result, CustomizeErrorCode.AJAX_RESULT_IS_NULL.getFullMessage());

        Long loginId = Long.valueOf(result.getResultObj().toString());
        Date date = new Date();

        // 保存 employee
        Employee em2Db = new Employee();
        em2Db.setEmail(employee.getEmail());
        em2Db.setTel(employee.getTel());
        em2Db.setLoginId(loginId);
        em2Db.setInputTime(date);
        em2Db.setType(EmployeeType.TYPE_ORG_ADMIN);
        employeeService.insert(em2Db);

        // 保存 tenant
        tenant.setAdminId(em2Db.getId());
        tenant.setRegisterTime(date);
        tenant.setState(TenantStateConstants.STATE_UN_AUDIT);
        tenantService.insert(tenant);
    }

    @Override
    @Transactional(rollbackFor = {Exception.class}) //需要加分布式事务
    public void audit(AuditDto auditDto) {

    }
}
